﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace FrameWorkSystem.GUI
{
    public partial class frmConnectionSetting : DevComponents.DotNetBar.Office2007Form
    {
        public string ConnectionString = "";
        public frmConnectionSetting()
        {
            InitializeComponent();
        }

        private void frmConnectionSetting_Load(object sender, EventArgs e)
        {
            this.ActiveControl = txt_Server;
        }     

        private void chkSqlAuthen_CheckedChanged(object sender, EventArgs e)
        {
            txt_UserName.ReadOnly = !chkSqlAuthen.Checked;
            txt_Password.ReadOnly = !chkSqlAuthen.Checked;
            if (chkSqlAuthen.Checked)
            {
                this.ActiveControl = txt_UserName;
                txt_UserName.BackColor = Color.White;
                txt_Password.BackColor = Color.White;
            }
            else
            {
                txt_UserName.BackColor = SystemColors.ControlLight ;
                txt_Password.BackColor = SystemColors.ControlLight;
            }
        }

        private void btnCheckConnection_Click(object sender, EventArgs e)
        {
            StringBuilder ConnString = new StringBuilder("user id=<uid>;password=<pwd>;server=<sv>; Integrated Security=<am>;Initial Catalog=<db>; Connection Timeout = 30");
            ConnString.Replace("<sv>", txt_Server.Text.Trim());
            ConnString.Replace("<db>", txt_Db.Text.Trim());
            if (chkSqlAuthen.Checked)
            {
                ConnString.Replace("<uid>", txt_UserName.Text.Trim());
                ConnString.Replace("<pwd>", txt_Password.Text.Trim());
                ConnString.Replace("<am>", "false");
            }
            else
            {
                ConnString.Replace("<uid>", "");
                ConnString.Replace("<pwd>", "");
                ConnString.Replace("<am>", "true");
            }
            SqlConnection sqlConn = new SqlConnection(ConnString.ToString());
            try
            {
                sqlConn.Open();
                ConnectionString = sqlConn.ConnectionString;
                MessageBox.Show("Kết nối thành công");
                this.DialogResult = DialogResult.OK;
            }
            catch
            {
                MessageBox.Show("Kết nối không thành công");
            }
            finally
            {
                sqlConn.Close();
            }            
        }
    }
}
